package com.yunsc.admin.controller.store;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.order.dto.OrderExportDTO;
import com.yunsc.order.dto.QueryRechargeOrderDTO;
import com.yunsc.order.entity.YunscOrderExportEntity;
import com.yunsc.order.entity.YunscRechargeOrderEntity;
import com.yunsc.order.sevice.*;
import com.yunsc.order.vo.*;
import com.yunsc.store.dto.QueryPageDTO;
import com.yunsc.store.dto.QueryUserParamsDTO;
import com.yunsc.store.dto.StoreHelpDTO;
import com.yunsc.store.entity.YunscHelpEntity;
import com.yunsc.store.entity.YunscRegionEntity;
import com.yunsc.store.entity.YunscStoreEntity;
import com.yunsc.store.entity.YunscUserBalanceLogEntity;
import com.yunsc.store.service.YunscHelpService;
import com.yunsc.store.service.YunscRegionService;
import com.yunsc.store.service.YunscStoreService;
import com.yunsc.store.service.YunscUserBalanceLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-04-04 22:00
 */
@Api(value = "商家信息接口", tags = "商家信息接口")
@Slf4j
@RestController
public class StoreController {

    @Resource
    private YunscStoreService storeService;
    @Resource
    private YunscRegionService regionService;
    @Resource
    private YunscOrderService orderService;
    @Resource
    private ExportOrderService exportOrderService;
    @Resource
    private YunscOrderExportService orderExportService;
    @Resource
    private YunscRechargeOrderService responseService;
    @Resource
    private YunscUserBalanceLogService userBalanceLogService;
    @Resource
    private YunscHelpService helpService;
    @Resource
    private YunscOrderGoodsService orderGoodsService;
    // 导出excel
    @ApiOperation(value = "导出订单", notes = "导出订单", httpMethod = "GET")
    @GetMapping("/order/export/exportOrder")
    public Result<?> export(OrderExportDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        exportOrderService.exportOrder(params, storeId);
        return Result.ok("导出成功");
    }

    // order/export/list
    @ApiOperation(value = "查询导出列表", notes = "查询导出列表", httpMethod = "GET")
    @GetMapping("/order/export/list")
    public Result<?> getOrderExportList(QueryPageDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscOrderExportEntity> list = orderExportService.queryOrderExportList(params, storeId);
        return Result.resultPageList(list);
    }

    @ApiOperation(value = "查询商家数据", notes = "查询商家数据", httpMethod = "GET")
    @GetMapping("/data")
    public Result<Map<String, Object>> storeData() {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        // 获取订单数据
        OrderOverviewVO overview = orderService.queryOrderOverview(currentStoreId);
        // 获取近七日交易走势
        TradeTrendVO tradeTrend = orderService.queryOrderTradeTrend(currentStoreId);
        // 获取消费情况统计（待发货订单(笔)、售后单(笔)、待付款订单(笔)、已售罄商品）
        ConsumptionVO statistics = orderService.queryConsumption(currentStoreId);
        // 获取订单状态统计
        OrderPendingVO orderPending = orderService.queryOrderPending(currentStoreId);

        Map<String, Object> storeData = new HashMap<>();
        storeData.put("overview", overview);
        storeData.put("statistics", statistics);
        storeData.put("pending", orderPending);
        storeData.put("tradeTrend", tradeTrend);
        Map<String, Object> map = new HashMap<>();
        map.put("data", storeData);
        return Result.ok(map);
    }

    @ApiOperation(value = "查询商家信息", notes = "查询商家信息", httpMethod = "GET")
    @GetMapping("/info")
    public Result<?> storeInfo() {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        YunscStoreEntity storeEntity = storeService.queryStoreInfo(currentStoreId);
        Map<String, Object> map = new HashMap<>();
        map.put("storeInfo", storeEntity);
        return Result.ok(map);
    }

    @ApiOperation(value = "修改商家信息", notes = "修改商家信息", httpMethod = "POST")
    @PostMapping("/update")
    public Result<?> updateStoreInfo(@RequestBody Map<String, YunscStoreEntity> storeInfo) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        storeService.updateStoreInfo(storeInfo.get("form"), currentStoreId);
        return Result.ok();
    }


    @ApiOperation("查询树形节点地址")
    @GetMapping("/region/tree")
    public Result<?> queryRegionTreeAddress() {
        Map<Integer, YunscRegionEntity> list = regionService.queryRegionTreeAddress();
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return Result.ok(map);
    }


    // /store/user/recharge/order?page=1
    @ApiOperation("查询充值记录")
    @GetMapping("/user/recharge/order")
    public Result<?> queryRechargeOrder(QueryRechargeOrderDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscRechargeOrderEntity> userInfoDto = responseService.queryRechargeOrder(param, storeId);
        return Result.resultPageList(userInfoDto);
    }

    @ApiOperation("查询用户余额日志")
    @GetMapping("/user/balance/log")
    public Result<?>  queryUserBalanceLog(QueryUserParamsDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscUserBalanceLogEntity> userBalanceLog = userBalanceLogService.queryBalanceLog(param,storeId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", userBalanceLog);
        return Result.resultPageList(userBalanceLog);
    }

    // store/content/help/list
    @ApiOperation(value = "查询帮助中心列表", notes = "查询帮助中心列表", httpMethod = "GET")
    @GetMapping("/content/help/list")
    public Result<?> queryHelpList(StoreHelpDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscHelpEntity> list = helpService.queryHelpList(params.getPage(), storeId);
        return Result.resultPageList(list);
    }

    // store/content/help/edit

    @ApiOperation(value = "编辑帮助中心", notes = "编辑帮助中心", httpMethod = "POST")
    @PostMapping("/content/help/edit")
    public Result<?> editHelp(@RequestBody StoreHelpDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        helpService.editHelp(params, storeId);
        return Result.ok();
    }


    // store/content/help/delete
    @ApiOperation(value = "删除帮助中心", notes = "删除帮助中心", httpMethod = "POST")
    @PostMapping("/content/help/delete")
    public Result<?> deleteHelp(@RequestBody StoreHelpDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        helpService.deleteHelp(params.getHelpId(), storeId);
        return Result.ok();
    }

    // store/content/help/add
    @ApiOperation(value = "新增帮助中心", notes = "新增帮助中心", httpMethod = "POST")
    @PostMapping("/content/help/add")
    public Result<?> addHelp(@RequestBody StoreHelpDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        helpService.addHelp(params, storeId);
        return Result.ok();
    }


    // /store/statistics.data/data
    @ApiOperation(value = "查询商家数据", notes = "查询商家数据", httpMethod = "GET")
    @GetMapping("/statistics/data/data")
    public Result<?> storeStatisticsData() {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        // 数据概况
        OverviewVO overview = orderService.queryOverview(storeId);
        // 获取近七日交易走势
        TradeTrendVO tradeTrend = orderService.queryOrderTradeTrend(storeId);
        // 商品销售榜
        List<GoodsRankingVO> goodsRanking = orderGoodsService.queryGoodsRanking(storeId);
        // 用户消费榜
        List<UserExpendRankingVO> userExpendRanking = orderService.queryUserExpendRanking(storeId);
        Map<String, Object> staticData = new HashMap<>();
        staticData.put("overview", overview);
        staticData.put("tradeTrend", tradeTrend);
        staticData.put("goodsRanking", goodsRanking);
        staticData.put("userExpendRanking", userExpendRanking);
        Map<String, Object> data = new HashMap<>();
        data.put("data", staticData);
        return Result.ok(data);
    }

    // /statistics/data/survey
    @ApiOperation(value = "查询商家数据", notes = "查询商家数据", httpMethod = "GET")
    @GetMapping("/statistics/data/survey")
    public Result<?> storeSurveyData(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate ) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        OverviewVO overview = orderService.queryOverviewByDate(startDate, endDate, storeId);
        return Result.ok(overview);
    }

}
